package com.kangwd.controller;

import com.kangwd.utils.DocConvertUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

@Controller

public class FileController {
    @RequestMapping("/openOffice")
    public String openOffice() {
        System.out.println("访问成功!");
        return "openOffice";
    }

    /**
     * pdf文件预览
     * @param filename
     * @param request
     * @param response
     */
    @RequestMapping("/getPdf")
    public void pdfView(String filename, HttpServletRequest request, HttpServletResponse response) {
        try {
            String fileUrl = request.getSession().getServletContext().getRealPath("/upload");
            //查看文件
            File sourceFile = new File(fileUrl + File.separator + filename);
            if (sourceFile.exists()) {
                //生成pdf
                String pdfFile = DocConvertUtil.doc2pdf(sourceFile.getPath());
                writePdf(pdfFile, response);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 文件预览页面
     * @return
     */
    @RequestMapping("/getFilePreviewPage")
    public String getFilePreviewPage() {
        return "openOffice";
    }

    /**
     * 上传文件
     * @param file
     * @param request
     * @param response
     * @return
     */
    @RequestMapping("/uploadFile")
    @ResponseBody
    public Map uploadFile(MultipartFile file, HttpServletRequest request, HttpServletResponse response) {
        Map resultMap = new HashMap();
        if (null == file || file.isEmpty()) {

        } else {
            String fileUrl = request.getSession().getServletContext().getRealPath("/upload");
            File basePath = new File(fileUrl);
            if (!basePath.exists()) {
                basePath.mkdirs();
            }
            String sourceFilename = file.getOriginalFilename();
            String fileSuffix = sourceFilename.substring(sourceFilename.lastIndexOf(".") + 1);
            String newFilename = UUID.randomUUID().toString() + "." + fileSuffix;
            String fileSource = fileUrl + File.separator +  newFilename;
            try {
                file.transferTo(new File(fileSource));
                resultMap.put("filename", newFilename);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return resultMap;
    }

    /**
     * swf文件预览
     * @param filename
     * @param request
     * @param response
     * @throws Exception
     */
    @RequestMapping("/getSwf")
    public void swfView(String filename, HttpServletRequest request, HttpServletResponse response){
        String fileUrl = request.getSession().getServletContext().getRealPath("/upload");
        File basePath = new File(fileUrl);
        if (!basePath.exists()) {
            basePath.mkdirs();
        }
        //查看文件
        File sourceFile = new File(fileUrl + File.separator + filename);
        if (sourceFile.exists()) {
            //生成pdf
            String pdfFile = null;
            try {
                pdfFile = DocConvertUtil.doc2pdf(sourceFile.getPath());
            } catch (Exception e) {
                e.printStackTrace();
            }
            //生成swf
            String swfFile = null;
            try {
                swfFile = DocConvertUtil.pdf2swf(pdfFile);
            } catch (Exception e) {
                e.printStackTrace();
            }
            writeSwf(swfFile, response);
        }
    }
    @RequestMapping("/eChart")
    public String eChart() {
        return "echarts";
    }
    /**
     * 输出pdf流
     * @param file 文件
     * @param response
     */
    private void writePdf(String file, HttpServletResponse response) {
        response.setContentType("application/pdf;charset=UTF-8");
        FileInputStream in = null;
        OutputStream out = null;
        try {
            in = new FileInputStream(file);
            out = response.getOutputStream();
            int count = 0;
            byte[] buffer = new byte[1024 * 1024];
            while ((count = in.read(buffer)) != -1) {
                out.write(buffer, 0, count);
            }
            out.flush();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (null != out) {
                    out.close();
                }
                if (null != in) {
                    in.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    /**
     * 输出swf流
     * @param file 文件
     * @param response
     */
    private void writeSwf(String file, HttpServletResponse response) {
        response.setContentType("application/x-shockwave-flash"); //text/plain
        FileInputStream in = null;
        OutputStream out = null;
        try {
            in = new FileInputStream(file);
            out = response.getOutputStream();
            int count = 0;
            byte[] buffer = new byte[1024 * 1024];
            while ((count = in.read(buffer)) != -1) {
                out.write(buffer, 0, count);
            }
            out.flush();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (null != out) {
                    out.close();
                }
                if (null != in) {
                    in.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
